﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BSCMFF.Core.MembershipDataProvider;
using BSCMFF.Core.DataAccessLayer;
using System.Collections;

namespace BSCMFF.Core.BusinessLayer
{
    public class UserManager
    {
        public void Validate()
        {
            EntityMembershipDataProvider ep = new EntityMembershipDataProvider();
            
                ep.ValidateUser("da", "das");
            
        }

        public int CreateUser(User newUser)
        {
            BSCEntities context = new BSCEntities();
            context.Users.AddObject(newUser);
            context.SaveChanges();
            return newUser.ID;
        }

        public bool Delete(int deletedUserID)
        {
            BSCEntities context = new BSCEntities();
            
            User u = (from user in context.Users
                      where user.ID == deletedUserID
                      select user).Single();

           
            context.Users.DeleteObject(u);
            try
            {
                context.SaveChanges();
            }
            catch(Exception e)
            {
                throw (e);
                //return false;
            }
            return true;
        }

        public User Update(User u)
        {
            BSCEntities context = new BSCEntities();
            User oldUser = (from user in context.Users
                      where user.ID == u.ID
                      select user).Single();


            oldUser.Login = u.Login;
            oldUser.Firstname = u.Firstname;
            oldUser.Lastname = u.Lastname;
            oldUser.Email = u.Email;
            oldUser.Role_FK = u.Role_FK;
            oldUser.Password = u.Password; // ht["password"] == null ? u.Login : ht["password"].ToString();
           // oldUser.Role_FK = Convert.ToInt32(ht["role_fk"] == null ? u.Login : ht["role_fk"]);
            //context.Attach(updatedUser);
            context.SaveChanges();
            return u;

        }

        public IList<User> GetAllUsers()
        {
            BSCEntities context = new BSCEntities();
            List<User> users = (from user in context.Users
                                select user).ToList();

            return users;
        }

        public IList<User> GetAllUsersByDepartment(int SBU_ID)
        {
            //SBU_User su; su.
            BSCEntities context = new BSCEntities();
            //List<User>
            //var users = (from user in context.Users.Include("SBU_User")
            //                    where user.SBU_FK == SBU_ID
            //                    select user).ToList();

            var users = (from user in context.Users
                         where user.SBU_FK == SBU_ID
                         select user).ToList();

            return users;
        }

        public User GetUserByID(int userID)
        {
            BSCEntities context = new BSCEntities();
            return (from u in context.Users
                    where u.ID == userID
                    select u).SingleOrDefault();
        }

        public User GetUserByLogin(string login)
        {
            BSCEntities context = new BSCEntities();
            return (from u in context.Users
                    where u.Login == login
                    select u).SingleOrDefault();
        }

        
    }
}
